﻿#pragma once


#ifdef AS_REDIS
extern "C"{
#include "hiredis/hiredis.h"
}
#include <string>
#include <map>
#include <unordered_map>
#include <vector>

namespace as
{
namespace db
{


enum class RedisError : uint8_t
{
	REDIS_SUCCESS = 0,
	REDIS_TIMEOUT = 1,
	REDIS_FAILURE = 2,
};

class RedisValue;
class RedisResult
{
public:
	RedisResult(redisReply *reply) : reply_(reply)
	{
	}

	RedisResult* SetReply(redisReply *reply);
public:
	RedisResult* SetError(const char *str);
	RedisResult* SetErrorCode(RedisError errorcode);
	RedisResult* SetCommand(std::string &cmd);

	const std::string &GetErrorDesc()const;
	RedisError GetErrrorCode()const;
	const std::string &GetCommand()const;
	int GetElementCount()const;
public:
	bool GetResult(bool &out);
	bool GetResult(int &out);
	bool GetResult(unsigned int &out);
	bool GetResult(int64_t &out);
	bool GetResult(uint64_t &out);
	bool GetResult(double &out);
	bool GetResult(float &out);
	bool GetResult(std::string &out);
	bool GetResult(RedisValue &out);
	bool GetResult(std::map<std::string, RedisValue> &out);
	bool GetResult(std::unordered_map<std::string, RedisValue> &out);
	bool GetResult(std::vector<RedisValue> &out);
	static bool MoveResult(redisReply *reply, RedisValue &out);

private:
	std::string command_{ "" };
	std::string error_desc_{ "" };
	RedisError error_code_{RedisError::REDIS_SUCCESS};
	redisReply *reply_ = nullptr;
};


}
}



#endif
